
import Vue from 'vue';
import Router from 'vue-router';
import HomePage from "../views/Home/HomePage.vue";
import WorkBenchPage from '../views/Workbench/WorkPage.vue';
import MyProfile from '../views/My/MyPage.vue';
import LoginPage from "../views/Login/LoginPage.vue"



import { isAuthenticated } from '@/utils/auth';

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/home',
      name: 'home',
      component: HomePage,
      meta: { requiresAuth: true },
    },
    {
      path: '/workbench',
      name: 'workbench',
      component: WorkBenchPage,
      meta: { requiresAuth: true },
    },
    {
      path: '/my',
      name: 'my',
      component: MyProfile,
      meta: { requiresAuth: true },
    },
    {
      path: '/login',
      name: 'login',
      component: LoginPage,
    },
  ],
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 如果目标页面需要认证且用户未登录
  if (to.matched.some(record => record.meta.requiresAuth) && !isAuthenticated()) {
    next({ name: 'login' }); // 跳转到登录页
  } else {
    next();
  }
});



export default router;
